package com.dragee.modules.work.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dragee.common.PageUtils;
import com.dragee.constants.CommonConstants;
import com.dragee.modules.work.dao.OrderGroupDao;
import com.dragee.modules.work.entity.OrderEntity;
import com.dragee.modules.work.entity.OrderGroupEntity;
import com.dragee.modules.work.entity.ShopProjectEntity;
import com.dragee.modules.work.po.OrderStatus;
import com.dragee.modules.work.service.OrderGroupService;
import com.dragee.modules.work.service.OrderService;
import com.dragee.modules.work.service.ShopProjectService;
import com.dragee.utils.IdUtils;
import com.dragee.utils.WrapperUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service("orderGroupService")
public class OrderGroupServiceImpl extends ServiceImpl<OrderGroupDao, OrderGroupEntity> implements OrderGroupService {


    @Resource
    private OrderGroupDao orderGroupDao;
    @Autowired
    private ShopProjectService shopProjectService;
    @Autowired
    private OrderService orderService;

    private String business = "orderGroup";
    /**
    * 列表
    */
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        int limit = params.get(CommonConstants.LIMIT) == null
                ?CommonConstants.LIMIT_SIZE:Integer.parseInt(params.get(CommonConstants.LIMIT).toString());
        int start = params.get(CommonConstants.START) == null
                ?CommonConstants.START_SIZE:Integer.parseInt(params.get(CommonConstants.START).toString());
        QueryWrapper<OrderGroupEntity> wrapper =
                WrapperUtil.getWrapper(business, params, OrderGroupEntity.class);
        IPage<OrderGroupEntity> page = new Page<>(start, limit);
        page = baseMapper.selectPage(page, wrapper.select());
        return new PageUtils(page);
    }

    @Override
    public void setOrderGrouping(OrderEntity order, String groupId) {
        OrderGroupEntity og = new OrderGroupEntity();
        ShopProjectEntity project = shopProjectService.getById(order.getProjectId());
        if (StrUtil.isBlank(groupId)) {
            groupId = IdUtils.getId().toString();
            og.setRoleName("团长");
        } else {
            og.setRoleName("团员");
        }
        og.setShopId(order.getShopId());
        og.setGroupId(groupId);
        og.setProjectId(order.getProjectId());
        og.setCustomer(order.getCustomer());
        og.setOrderId(order.getId().toString());
        og.setCustomerTel(order.getCustomerTel());
        og.setNeedNum(project.getPtPersonTotal());
        og.setStatus("未成团");
        saveOrUpdate(og);
        // 试图成团
        attemptFinishGroup(groupId);
    }

    /** 尝试成团
     * @return
     */
    private boolean attemptFinishGroup (String groupId) {
        List<OrderGroupEntity> datas = list(new QueryWrapper<OrderGroupEntity>().eq("groupId", groupId));
        OrderGroupEntity oneGroup = datas.get(0);
        if (Integer.parseInt(oneGroup.getNeedNum()) == datas.size()) {
            // 可以成团
            for (OrderGroupEntity data : datas) {
                data.setStatus("已成团");
                saveOrUpdate(data);
                data.getOrderId();
                OrderEntity order = orderService.getById(data.getOrderId());
                order.setStatus(OrderStatus.waitShopConfirm.getValue());
                orderService.saveOrUpdate(order);
            }
            return true;
        }
        return false;
    }


}